package com.myfum.mobile.tetris.views
{
	import com.myfum.mobile.tetris.data.TetrisMap;
	import com.myfum.mobile.tetris.data.shapes.ShapeFactory;
	import com.myfum.mobile.tetris.events.P2PEvent;
	import com.myfum.mobile.tetris.utils.P2PHelper;
	
	import flash.events.MouseEvent;
	
	import org.robotlegs.mvcs.Mediator;
	
	public class MenuBarMediator extends Mediator
	{
		[Inject]
		public var view:MenuBar;
		[Inject]
		public var map:TetrisMap;
		[Inject]
		public var p2p:P2PHelper;
		
		public function MenuBarMediator()
		{
			super();
		}
		
		override public function onRegister():void{
			view.mouseChildren = true;
			view.mouseEnabled = true;
			eventMap.mapListener(view.start, MouseEvent.CLICK, onStart);
			eventMap.mapListener(view.connect, MouseEvent.CLICK, onConnect);
			eventMap.mapListener(eventDispatcher, P2PEvent.NET_STATES_CHANGED, onStatesChanged);
		}
		
		private function onStatesChanged(e:P2PEvent):void{
			view.txt.appendText(e.info.toString() + '\n');
		}
		
		private function onStart(e:MouseEvent):void{
			map.appendShape(ShapeFactory.createRandomShape());
		}
		
		private function onConnect(e:MouseEvent):void{
			p2p.connect();
		}
	}
}